﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

namespace goMatic
{
    public partial class DIMMER : System.Web.UI.UserControl
    {
        public event EventHandler LevelChanged;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public DIMMER()
        {
        }

        v_HMDeviceDetails vDetail;
        public v_HMDeviceDetails _Details
        {
            get { return vDetail; ; }
            set
            {
                if (value != null)
                {
                    vDetail = value;
                    dName.Text = vDetail.Name;
                    lDevice.Text = vDetail.Address;
                    if (vDetail.value != null)
                    {
                        _Level = double.Parse(vDetail.value, System.Globalization.CultureInfo.InvariantCulture) * 100;
                        TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time");
                        TimeSpan offset = tzi.GetUtcOffset(DateTime.Now);
                        lTime.Text = (value.C__createdAt.Value.LocalDateTime + offset).ToString("dd-MM HH:mm:ss");
                    }
                    else
                    {
                        Image2.ImageUrl = "~/Images/unknown.png";
                    }
                }
                else
                {
                    Image2.ImageUrl = "~/Images/unknown.png";
                }
            }
        }


        internal double dLevel;
        public double _Level
        {
            get
            {
                return dLevel;
            }
            set
            {
                dLevel = value;
                tb_Dimmer.Text = dLevel.ToString("N0");

                if (dLevel > 0)
                {
                    Image2.ImageUrl = "~/Images/light_light_dim_100_32.png";
                    if(dLevel < 100)
                        lLevel.Text = dLevel.ToString("N0") + "%";
                    else
                        lLevel.Text = "ON";
                }
                else
                {
                    Image2.ImageUrl = "~/Images/light_light_32.png";
                    lLevel.Text = "OFF";
                }
            }
        }

        public bool ReadOnly { get; set; }

        protected void tb_Dimmer_TextChanged(object sender, EventArgs e)
        {
            double dNew = double.Parse(tb_Dimmer.Text,System.Globalization.CultureInfo.InvariantCulture);
            
            if(dNew == _Level)
                return;

            _Level = dNew;
            if (this.LevelChanged != null)
            {
                this.LevelChanged(this, e);
            }
            if (_Level != null)
            {
                if (!ReadOnly)
                {
                    CCU.ccucmdSoapClient oCMD = new CCU.ccucmdSoapClient();

                    NumberFormatInfo nfi = new NumberFormatInfo();
                    nfi.NumberDecimalSeparator = ".";
                    nfi.CurrencyDecimalDigits = 6;

                    oCMD.SetValueAsync("", vDetail.Interface, vDetail.Address, "LEVEL", "double", (_Level / 100).ToString(nfi), vDetail.CustomerID);
                }
            }
            else
            {
                //Get value
            }

        }
    }
}